package cst.a128lqn.startproject128.model;
//import cst.a128lqn.startproject128.bean.WeatherResult;
import cst.a128lqn.startproject128.bean.WeatherResult;
import cst.a128lqn.startproject128.iface.WeatherIface;
import cst.a128lqn.startproject128.listener.WeatherListener;
import cst.a128lqn.startproject128.service.Weather51Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/2/0002.
 */

public class Weather51Model  implements WeatherIface {
    private Weather51Service service;
    private  Retrofit retrofit;
    private static final String BASE_URL = "http://weather.51wnl.com/weatherinfo/";

    public Weather51Model(){

        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();


    }

    @Override
    public void getWeather(String citynumber, final WeatherListener listener) {
        Weather51Service service = retrofit.create(Weather51Service.class);
        Call<WeatherResult> call = service.getResult(citynumber,1);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body().getWeatherinfo());
                }
                else{
                    listener.onFail("Error！");
                }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}